package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dto.PostDto;
import com.example.demo.entity.Post;
import com.example.demo.mapper.PostMapper;
import com.example.demo.service.BalanceFoodRelationService;
import com.example.demo.service.PostService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author Atanycosts
 * @description 针对表【post】的数据库操作Service实现
 * @createDate 2023-04-14 22:10:53
 */
@Scope("prototype")
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post>
        implements PostService {


    @Autowired
    @Lazy //懒加载
    private PostService postService;

    @Autowired
    private BalanceFoodRelationService balanceFoodRelationService;

    /**
     * 新增帖子
     *
     * @param post
     */
    @Override
    public void savePost(Post post) {
        postService.save(post);
    }

    /**
     * 修改更新帖子
     *
     * @param post
     */
    @Override
    public void updatePost(Post post) {
        // LambdaQueryWrapper<Post> postLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // postLambdaQueryWrapper.eq(Post::getId, post.getId());
        // postService.update(postLambdaQueryWrapper);
        //根据主键ID去改
        postService.updateById(post);
    }

    /**
     * 删除帖子
     *
     * @param id
     */
    @Override
    public void deletePost(Long id) {
        //根据主键ID去改
        postService.removeById(id);
    }

    /**
     * 根据ID找帖子
     *
     * @param id
     * @return
     */
    @Override
    public PostDto getByIdPost(Long id) {
        PostDto postDto = new PostDto();
        //通过ID找到数据Post
        Post byId = this.getById(id);
        //拷贝进去
        BeanUtils.copyProperties(byId, postDto);
        //拿到BalanceFoodRelationId
        Long balanceFoodRelationId = byId.getBalanceFoodRelationId();
        //调用写好的根ID据查询方案接口,并传入balanceFoodRelationId
        postDto.setBalanceDto(
                Collections.singletonList(balanceFoodRelationService
                        .getByIdBalance(
                                balanceFoodRelationId)));
        return postDto;
    }

    /**
     * 根据帖子标题找/分页查询
     *
     * @param page
     * @param pageSize
     * @param postName
     * @return
     */
    @Override
    public Page<Post> getAllPost(int page, int pageSize, String postName) {

        //构造分页构造器
        Page<Post> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(postName), Post::getPostTitle, postName);
        //添加排序条件
        queryWrapper.orderByDesc(Post::getPostUpdateTime);

        //执行查询
        postService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    /**
     * AI智能帖子推荐
     * @return
     */
    @Override
    public List<Post> recommendAi() {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByDesc(Post::getPostView);
        List<Post> list = postService.list(queryWrapper);
        return list;
    }


}




